package com.ianswer.px.user.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ianswer.px.common.SessionContextHolder;
import com.ianswer.px.user.entity.UsersEdu;
import com.ianswer.px.user.entity.UsersFigure;
import com.ianswer.px.user.entity.UsersInterests;
import com.ianswer.px.user.entity.UsersWorkExperience;
import com.ianswer.px.user.service.UserFigureService;
import com.ianswer.px.user.service.UsersEduService;
import com.ianswer.px.user.service.UsersInterestsService;
import com.ianswer.px.user.service.UsersWorkExpService;

@Controller
public class UserInfoController {

	@Resource(name = "userFigureService")
	private UserFigureService userFigureService;

	@Resource(name = "usersWorkExpService")
	private UsersWorkExpService usersWorkExpService;

	@Resource(name = "usersEduService")
	private UsersEduService usersEduService;

	@Resource(name = "usersInterestsService")
	private UsersInterestsService usersInterestsService;

	@RequestMapping(value = "/user/info", method = RequestMethod.GET)
	public String baseinfo(Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		UsersFigure f = userFigureService.get(userId);
		if (f != null) {
			model.addAttribute("figure", f);
		}
		return "/user/infoedit";
	}

	@RequestMapping(value = "/user/saveinfo", method = RequestMethod.POST)
	public String saveInfo(UsersFigure exp, Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		exp.setUserId(userId);
		userFigureService.save(exp);
		model.addAttribute("figure", exp);
		return "user/infoedit";
	}

	@RequestMapping(value = "/user/savework", method = RequestMethod.POST)
	public String saveWorkExp(UsersWorkExperience exp, Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		exp.setUserId(userId);
		usersWorkExpService.save(exp);
		model.addAttribute("work", exp);
		return "user/workedit";
	}

	@RequestMapping(value = "/user/saveinterests", method = RequestMethod.POST)
	public String saveInterests(UsersInterests i, Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		i.setUserId(userId);
		usersInterestsService.save(i);
		model.addAttribute("interests", i);
		return "user/interestsedit";
	}

	@RequestMapping(value = "/user/saveedu", method = RequestMethod.POST)
	public String saveEdu(UsersEdu e, Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		e.setUserId(userId);
		usersEduService.save(e);
		model.addAttribute("edu", e);
		return "user/eduedit";
	}

	@RequestMapping(value = "/user/workedit", method = RequestMethod.GET)
	public String queryWorkExp(Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		UsersWorkExperience r = usersWorkExpService.query(userId);

		model.addAttribute("work", r);
		return "user/workedit";
	}

	@RequestMapping(value = "/user/interestsedit", method = RequestMethod.GET)
	public String queryInterests(Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		UsersInterests r = usersInterestsService.get(userId);

		model.addAttribute("interests", r);
		return "user/interestsedit";
	}

	@RequestMapping(value = "/user/eduedit", method = RequestMethod.GET)
	public String queryEdu(Model model) {
		Long userId = SessionContextHolder.currentUser().getUserId();
		UsersEdu r = usersEduService.get(userId);

		model.addAttribute("edu", r);
		return "user/eduedit";
	}

	@RequestMapping(value = "/user/{userId}/info", method = RequestMethod.GET)
	public String info(@PathVariable("userId") long userId, Model model) {
		UsersFigure f = userFigureService.get(userId);
		if (f != null) {
			model.addAttribute("figure", f);
		}

		UsersWorkExperience w = usersWorkExpService.get(userId);
		model.addAttribute("work", w);

		UsersEdu e = usersEduService.get(userId);
		model.addAttribute("edu", e);

		UsersInterests i = usersInterestsService.get(userId);
		model.addAttribute("interests", i);

		return "user/info";
	}

	//	
	//	@RequestMapping(value = "/user/{userId}/work", method = RequestMethod.GET)
	//	public String getWorkExp(@PathVariable("userId") long userId,Model model) {
	//		
	//		UsersWorkExperience r = usersWorkExpService.query(userId);
	//		
	//		model.addAttribute("work", r);
	//		return "user/work";
	//	}
	//	
	//	@RequestMapping(value = "/user/{userId}/edu", method = RequestMethod.GET)
	//	public String getEdu(@PathVariable("userId") long userId,Model model) {
	//		
	//		UsersEdu r = usersEduService.query(userId);
	//		
	//		model.addAttribute("edu", r);
	//		return "user/edu";
	//	}
	//	
	//	@RequestMapping(value = "/user/{userId}/interests", method = RequestMethod.GET)
	//	public String getInterests(@PathVariable("userId") long userId,Model model) {
	//		
	//		UsersInterests r = usersInterestsService.query(userId);
	//		
	//		model.addAttribute("interests", r);
	//		return "user/interests";
	//	}

}
